package com.biz.primus.management.promotion.controller;

import com.biz.primus.base.exception.BizBusinessException;
import com.biz.primus.base.vo.PageVo;
import com.biz.primus.base.vo.promotion.PromotionProductReqVo;
import com.biz.primus.common.utils.JsonUtils;
import com.biz.primus.common.vo.SpringPageVO;
import com.biz.primus.management.base.controller.AbstractBaseController;
import com.biz.primus.management.promotion.feign.PromotionFeignClient;
import com.biz.primus.management.promotion.service.PromotionSupportService;
import com.biz.primus.model.promotion.vo.PurchaseGiftPromotionVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

/**
 * @author david-liu
 */
@Controller
@RequestMapping(value = "/management/purchaseGiftPromotion")
public class PurchaseGiftPromotionController extends AbstractBaseController {

    @Autowired
    private PromotionSupportService promotionSupportService;

    @Autowired
    private PromotionFeignClient promotionFeignClient;

    @RequestMapping(value = "list")
    public ModelAndView list(PageVo pageVo) {
        SpringPageVO<PurchaseGiftPromotionVo> pageVO = promotionFeignClient.getPurchaseGiftPromotions(pageVo);
        return new ModelAndView("purchase_gift/list").addObject("page", pageVO);
    }

    @RequestMapping(value = "new")
    public ModelAndView create() {
        return new ModelAndView("purchase_gift/add");
    }

    @RequestMapping(value = "edit")
    public ModelAndView edit(Long id) {
        return new ModelAndView("purchase_gift/add").addObject("purchaseGift", promotionFeignClient.getPurchaseGiftPromotion(id));
    }

    @RequestMapping(value = "statusToggle")
    public ModelAndView statusToggle(Long id) {
        if (!promotionFeignClient.togglePurchaseGiftPromotionStatus(id)) {
            throw new BizBusinessException("切换活动状态出错");
        }
        return new ModelAndView("redirect:list.do");
    }

    @RequestMapping(value = "save", method = RequestMethod.POST)
    @ResponseBody
    public Boolean save(String data) {
        return promotionFeignClient.updatePurchaseGiftPromotion(JsonUtils.json2Obj(data, PurchaseGiftPromotionVo.class));
    }

    @RequestMapping(value = "/products")
    @ResponseBody
    public String promotionProducts(Long promotionId) {
        return JsonUtils.obj2Json(promotionSupportService.listPromotionProductsInUserFirstOrder(promotionId));
    }

    @RequestMapping(value = "/products/search")
    @ResponseBody
    public String searchPromotionProducts(PromotionProductReqVo reqVo) {
        return JsonUtils.obj2Json(promotionSupportService.searchPromotionProducts(reqVo));
    }

    @RequestMapping(value = "/coupons")
    @ResponseBody
    public String coupons(Long promotionId) {
        return JsonUtils.obj2Json(promotionSupportService.listGiftCouponsInPurchaseGiftPromotion(promotionId));
    }

    @RequestMapping(value = "/allCoupons")
    @ResponseBody
    public String allCoupons() {
        return JsonUtils.obj2Json(promotionSupportService.listAllGiftCoupons());
    }

}
